package leetcode.code1542;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int longestAwesome(String s) {
		char[] cs = s.toCharArray();
		Map<Integer, Integer> map = new HashMap<>();
		map.put(0, 0);
		int ans = 0;
		for (int i = 1, j = 0; i <= cs.length; i++) {
			int c = cs[i - 1] - '0';
			j ^= 1 << c;
			if (j == 0 || Integer.bitCount(j) == 1) {
				ans = Math.max(ans, i);
			} else {
				for (int k = 0; k < 10; k++) {
					int rest = 1 << k;
					ans = Math.max(ans, i - map.getOrDefault(j ^ rest, i));

				}
			}
			map.putIfAbsent(j, i);
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(5, this.longestAwesome("185801630663498"));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
